USE [simulasyon]
GO

/****** Object:  StoredProcedure [dbo].[FarkGuncelle]    Script Date: 04/24/2011 18:40:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Baran Kaynak
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[FarkGuncelle]
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    UPDATE Siparis 
SET SonSimBitFark = (SELECT     dbo.WorkTime(SimBitTarihi, SonTarih) AS Expr1
FROM         Siparis as Secilen Where Secilen.Id=Siparis.Id)
,
SimBitSonFark = (SELECT     dbo.WorkTime(SonTarih, SimBitTarihi) AS Expr1
FROM         Siparis as Secilen Where Secilen.Id=Siparis.Id)
,
SimFark = (SELECT     dbo.WorkTime(SimBasTarihi, SimBitTarihi) AS Expr1
FROM         Siparis as Secilen Where Secilen.Id=Siparis.Id)
,
CizFark = (SELECT     dbo.WorkTime(CizBasTarihi, CizBitTarihi) AS Expr1
FROM         Siparis as Secilen Where Secilen.Id=Siparis.Id)

--WHERE SonSimBitFark<0  OR SimBitSonFark<0 OR SimFark<0 OR CizFark<0

UPDATE    IsParcasi
SET              CizelgeFark = dbo.WorkTime(CizelgeBaslangic, CizelgeBitis), 
KuyrukFark = dbo.WorkTime(KuyrukGiris, KuyrukCikis), 
ToplamFark =
                          (SELECT      dbo.WorkTime(IsParcasiSecilen.KuyrukGiris, (CASE WHEN IsParcasi_1.KuyrukGiris IS NOT NULL 
                      THEN IsParcasi_1.KuyrukGiris ELSE IsParcasiSecilen.IslemCikis END)) AS Expr1
FROM         IsParcasi AS IsParcasiSecilen LEFT OUTER JOIN
                      IsParcasi AS IsParcasi_1 ON IsParcasiSecilen.OnundekiParcaId = IsParcasi_1.Id
                            WHERE      (IsParcasi.Id = IsParcasiSecilen.Id))
--WHERE     (CizelgeFark IS NULL) OR
--                      (KuyrukFark IS NULL) OR
--                      (IslemFark IS NULL) OR
--                      (ToplamFark IS NULL) OR
--                      (ToplamFark = 0)
END

GO

